package com.socool;


import java.util.ArrayList;
import java.util.Collection;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.facebook.FacebookException;
import com.facebook.FacebookOperationCanceledException;
import com.facebook.Session;
import com.facebook.model.GraphUser;
import com.facebook.widget.WebDialog;
import com.facebook.widget.WebDialog.OnCompleteListener;

public class Frag_MyFriend extends DSFragment {
	 

	View view;
	
	Button BT_appRequest;
	
	private String LOG_TAG = "test";
	
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
    	view = (LinearLayout) inflater.inflate(R.layout.frag_my_friend, container, false);
    	
    	BT_appRequest = (Button)view.findViewById(R.id.BT_appRequest);
    	BT_appRequest.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				 onClickPickFriends();
				
			}
		});
    	
        return view;
    }
    
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case PICK_FRIENDS_ACTIVITY:
                displaySelectedFriends(resultCode);
                break;
            default:
                Session.getActiveSession().onActivityResult(getActivity(), requestCode, resultCode, data);
                break;
        }
    }
    
    private void displaySelectedFriends(int resultCode) {
        
        FriendPickerApplication application = (FriendPickerApplication) getActivity().getApplication();

        Collection<GraphUser> selection = application.getSelectedUsers();
        if (selection != null && selection.size() > 0) {
        	String ids = "";
         
            for (GraphUser user : selection) {
            	if (!ids.equals("")) {
            		ids +=",";
            	}
            	ids += user.getId();
            }
            
            sendRequestDialog(ids);
            
        } else {
        	Log.d(LOG_TAG, "<No friends selected>");
            
        }

      //  resultsTextView.setText(results);
    }
    
    private static final int PICK_FRIENDS_ACTIVITY = 1;
    private void onClickPickFriends() {
        FriendPickerApplication application = (FriendPickerApplication) getActivity().getApplication();
        application.setSelectedUsers(null);

        Intent intent = new Intent(getActivity().getApplicationContext(), PickFriendsActivity.class);
        // Note: The following line is optional, as multi-select behavior is the default for
        // FriendPickerFragment. It is here to demonstrate how parameters could be passed to the
        // friend picker if single-select functionality was desired, or if a different user ID was
        // desired (for instance, to see friends of a friend).
        PickFriendsActivity.populateParameters(intent, null, true, true);
        startActivityForResult(intent, PICK_FRIENDS_ACTIVITY);
    }
    
    private void sendRequestDialog(String ids) {
    	//ids 에는 요청을 보낼 아이디들이 들어있다.
    	
        Bundle params = new Bundle();
        params.putString("message", LOG_TAG);
     //   params.putString("name", "X");
      //  String toIDs = "100002146175769,100002244773301";
        //params.putString("to", "100002244773301");
        
        WebDialog requestsDialog = (
            new WebDialog.RequestsDialogBuilder(getActivity(),
                Session.getActiveSession(),
                params)).setTo(ids)
                .setOnCompleteListener(new OnCompleteListener() {

                    @Override
                    public void onComplete(Bundle values,
                        FacebookException error) {
                        if (error != null) {
                            if (error instanceof FacebookOperationCanceledException) {
                                Toast.makeText(getActivity().getApplicationContext(), 
                                    "Request cancelled", 
                                    Toast.LENGTH_SHORT).show();
                            } else {
                                Toast.makeText(getActivity().getApplicationContext(), 
                                    "Network Error", 
                                    Toast.LENGTH_SHORT).show();
                            }
                        } else {
                        	Log.d(LOG_TAG, "request result: "+values);
                            final String requestId = values.getString("request");
                            if (requestId != null) {
                          
                       
                            /*	ArrayList<String> mArrayList_to = values.getStringArrayList("to");
                            	for(int i=0; i<mArrayList_to.size(); i++){
                            		Log.d(LOG_TAG, "mArrayList_to: "+mArrayList_to.get(i));
                            	}*/
                            	
                                Toast.makeText(getActivity().getApplicationContext(), 
                                    "Request sent",  
                                    Toast.LENGTH_SHORT).show();
                            } else {
                                Toast.makeText(getActivity().getApplicationContext(), 
                                    "Request cancelled", 
                                    Toast.LENGTH_SHORT).show();
                            }
                        }   
                    }

                })
                .build();
        requestsDialog.show();
    }
    
}

